<?php

namespace WeForge\Queue;

use HttpClient\Aliyun\FunctionCompute\Application as Fc;
use HttpClient\RequestException;
use Illuminate\Contracts\Queue\Queue as QueueContract;
use Illuminate\Queue\Queue;

class FcQueue extends Queue implements QueueContract
{
    /**
     * @var \HttpClient\Aliyun\ServerlessWorkflow\Application
     */
    protected $fnf;

    /**
     * The name of the default queue.
     *
     * @var string
     */
    protected $default;

    /**
     * Create a new MnsQueue instance.
     *
     * @param \HttpClient\Aliyun\ServerlessWorkflow\Application $fc
     * @param string $default
     */
    public function __construct(Fc $fc)
    {
        $this->fc = $fc;
        // $this->default = $default;
    }

    /**
     * Get the size of the queue.
     *
     * @param  string|null  $queue
     * @return int
     */
    public function size($queue = null)
    {
        return (int) $this->fc->queue->get($this->getQueue($queue))['ActiveMessages'];
    }

    /**
     * Push a new job onto the queue.
     *
     * @param  string|object  $job
     * @param  mixed  $data
     * @param  string|null  $queue
     * @return mixed
     */
    public function push($job, $data = '', $queue = null)
    {
        return $this->pushRaw(
            $this->createPayload($job, $this->getQueue($queue), $data),
            $queue
        );
    }

    /**
     * Push a raw payload onto the queue.
     *
     * @param  string  $payload
     * @param  string|null  $queue
     * @param  array  $options
     * @return mixed
     */
    public function pushRaw($payload, $queue = null, array $options = [])
    {
        $this->fc->function->invokeAsynchronously('weforge-weforge-staging', 'weforge-staging-cli', [
            'payload' => json_encode([
                'cli' => sprintf('php artisan weforge:work "fc" "%s"', base64_encode($payload)),
            ]),
        ]);
    }

    /**
     * Push a new job onto the queue after a delay.
     *
     * @param  \DateTimeInterface|\DateInterval|int  $delay
     * @param  string|object  $job
     * @param  mixed  $data
     * @param  string|null  $queue
     * @return mixed
     */
    public function later($delay, $job, $data = '', $queue = null)
    {
        throw new \LogicException('The delay job is not supported.');
        // throw new \LogicException();
        // $payload = $this->createPayload($job, $this->getQueue($queue), $data);

        // $this->fc->function->invokeAsynchronously('weforge-weforge-staging', 'weforge-staging-cli', [
        //     'payload' => json_encode([
        //         'delay' => $delay,
        //         'cli' => sprintf('php artisan weforge:work "fc" "%s"', base64_encode($payload)),
        //     ]),
        // ]);

        // dd($delay);
        // return $this->fc->queue_message->send(
        //     $this->getQueue($queue),
        //     base64_encode($this->createPayload($job, $this->getQueue($queue), $data)),
        //     $this->secondsUntil($delay)
        // )['MessageId'];
    }

    /**
     * Pop the next job off of the queue.
     *
     * @param  string  $queue
     * @return \Illuminate\Contracts\Queue\Job|null
     */
    public function pop($queue = null)
    {
        dd('f');
        return $this->receiveQueueMessage($queue = $this->getQueue($queue), function ($job) use ($queue) {
            return new MnsJob(
                $this->container,
                $this->fc,
                $job,
                $this->connectionName,
                $queue
            );
        });
    }

    /**
     * Get the underlying MNS instance.
     *
     * @return \HttpClient\Aliyun\ServerlessWorkflow\Application
     */
    public function getInstance()
    {
        return $this->fc;
    }

    /**
     * @param  string $queue
     * @return string
     */
    protected function getQueue($queue)
    {
        return $queue ?: $this->default;
    }
}
